window.onload = function () {
    var codeStr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    //获取div
    var oDiv = document.querySelector(".code");
    var oHuan = document.querySelector(".huan")
    //获取用户输入的验证码
    var oUser = document.querySelector(".word1");
    //获取提交的按钮
    var oTi = document.querySelector(".ti");
    //随机生成整数
    function getRandom(n, m) {
        n = Number(n);
        m = Number(m);
        //确保m大于n
        if (n > m) {
            var temp = n;
            n = m;
            m = temp
        }
        return Math.floor(Math.random() * (m - n) + n)
    }
    function getCode() {
        var str = "";
        for (var i = 0; i < 4; i++) {
            var ran = getRandom(0, 62);
            str += codeStr.charAt(ran);

        }
        oDiv.innerHTML = str
    }
    getCode();
    // 点击刷新验证码
    oDiv.onclick = function () {
        getCode();
    }
    oHuan.onclick = function () {
        getCode();
    }
    oTi.onclick = function () {
        //获取用户输入的验证码
        var userCode = oUser.value;
        //获取随机生成的验证码
        var suiCode = oDiv.value
        if (userCode == 0) {
            alert("请输入验证码")
            return false
        } else if (userCode != suiCode) {
            alert("验证码不正确,请你从新输入");
            oUser = " "
            getCode()
            return false

        } else {
            alert("提交成功")
            oUser = " "
        }
    }
}